package com.itheima.redbaby.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.R;



public class FaPiaoFillActivity extends BaseActivity implements OnClickListener {
	private RelativeLayout fapiao_geren_rel;
	private RelativeLayout fapiao_danwei_rel;
	private RelativeLayout fapiao_youxi_rel;
	private RelativeLayout fapiao_yinxiang_rel;
	private RelativeLayout fapiao_tushu_rel;
	private RelativeLayout fapiao_ruanjian_rel;
	private RelativeLayout fapiao_ziliao_rel;
	
	private View [] contentViews;
	private View [] titleViews;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fapiao_detail_info_activity);
		init();
	}
	
	private void init() {
		head_add_txt = (TextView) findViewById(R.id.head_add_txt);
		head_jiesuan_center = (TextView) findViewById(R.id.head_jiesuan_center);
		head_add_txt.setOnClickListener(this);
		head_jiesuan_center.setOnClickListener(this);
		
		fapiao_geren_rel = (RelativeLayout) FId(R.id.fapiao_geren_rel);
//		ImageView childAt2 = (ImageView) fapiao_geren_rel.getChildAt(2);
//		ImageView childAt1 = (ImageView) fapiao_geren_rel.getChildAt(1);
		fapiao_danwei_rel = (RelativeLayout) FId(R.id.fapiao_danwei_rel);
		titleViews = new View[] {fapiao_geren_rel,fapiao_danwei_rel};
		
		fapiao_tushu_rel = (RelativeLayout) FId(R.id.fapiao_tushu_rel);
		fapiao_yinxiang_rel = (RelativeLayout) FId(R.id.fapiao_yinxiang_rel);
		fapiao_youxi_rel = (RelativeLayout) FId(R.id.fapiao_youxi_rel);
		fapiao_ruanjian_rel = (RelativeLayout) FId(R.id.fapiao_ruanjian_rel);
		fapiao_ziliao_rel = (RelativeLayout) FId(R.id.fapiao_ziliao_rel);
		contentViews = new View[] {fapiao_tushu_rel,fapiao_yinxiang_rel,fapiao_youxi_rel,fapiao_ruanjian_rel,fapiao_ziliao_rel};
		
		for (int i = 0; i < titleViews.length; i++) {
			titleViews[i].setOnClickListener(this);
		}
		for (int i = 0; i < contentViews.length; i++) {
			contentViews[i].setOnClickListener(this);
		}
	}
	private int invoiceTitle=-1;
	private int invoiceContent=-1;
	private TextView head_add_txt;
	private TextView head_jiesuan_center;
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.fapiao_danwei_rel:
			invoiceTitle=0;
			//如果点击的是标题栏
			for(int i = 0; i < titleViews.length; i++) {
				if(v.getId()==titleViews[i].getId()){
					
					System.out.println("标题被点击");
					//1.显示当前图片
					show(((ViewGroup) titleViews[i]).getChildAt(1));
					continue;
					}
					//2.隐藏其他图片
					hide(((ViewGroup) titleViews[i]).getChildAt(1));
			}
			break;
		case R.id.fapiao_geren_rel:
			invoiceTitle=1;
			//如果点击的是标题栏
			for (int i = 0; i < titleViews.length; i++) {
				if(v.getId()==titleViews[i].getId()){
					
					System.out.println("标题被点击");
					//1.显示当前图片
					show(((ViewGroup) titleViews[i]).getChildAt(1));
					continue;
					}
					//2.隐藏其他图片
					hide(((ViewGroup) titleViews[i]).getChildAt(1));
			}
			break;
		case R.id.head_add_txt:
			if(invoiceTitle==-1||invoiceContent==-1){
				Toast.makeText(FaPiaoFillActivity.this, "请选择标题头和标题内容！", 0).show();
				return;
			}
			Intent data=new Intent();
			data.putExtra("invoiceTitle", invoiceTitle);
			data.putExtra("invoiceContent", invoiceContent);
			setResult(Activity.RESULT_OK, data);
			finish();
			break;
		case R.id.head_jiesuan_center:
			Intent intent=new Intent();
			intent.putExtra("invoiceTitle", invoiceTitle);
			intent.putExtra("invoiceContent", invoiceContent);
			setResult(Activity.RESULT_OK, intent);
			finish();
			break;
		default:
			for (int i = 0; i < contentViews.length; i++) {
				if(v.getId()==contentViews[i].getId()){
					invoiceContent=i;
					System.out.println("内容被点击");
					show(((ViewGroup) contentViews[i]).getChildAt(1));
					continue;
				}
				hide(((ViewGroup) contentViews[i]).getChildAt(1));
			}
		break;
		}
	}
		//隐藏
		private void hide(View childAt) {
			childAt.setVisibility(View.GONE);
		}
		//显示组件
		private void show(View childAt) {
			childAt.setVisibility(View.VISIBLE);
		}
}
